// xc_lshop/shop/coupon/index.js
var actcouponbill = require('../../../xc/model/coupon_cont')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    
  },

  xcloaddata: function (options, config) {
  
    var updateconfig={}
    if (config.prothem && config.prothem.themcolor) {
      var themconfig = config.prothem.themcolor.config;
      updateconfig['themconfig'] = themconfig
    }
    
    this.setData(updateconfig);
      
  },


  loaddata:function(){
    var that =this;
    actcouponbill.procoupon().then(res=>{  
      if(res["status"]==1){
        var modellist=[];
        if(  res["obj"]){
          modellist=res["obj"];
          var ilen =modellist.length
          for(var i=0;i<ilen;i++){
          

            if(modellist[i].cid ==1)
            {
              modellist[i].val= modellist[i].val
            }              
            modellist[i].fullmoney= modellist[i].fullmoney

            if(i<2)
            {
              var temtag=""
              if(  modellist[i].hascondition==1){
              
              }
              else
              {
                temtag+="满" +(modellist[i].fullmoney/100)
              }

              if(  modellist[i].cid==1){
                temtag+="减" +(modellist[i].val/100)
              }
              else
              {
                temtag+=modellist[i].val/10 +"折"
              }
              

            }
           


          }
        }
        
       

        that.setData({modellist:modellist})

      }
    })
  },


  afterlogin: function (options, config, userinfo) {
    this.loaddata();
    
  },

  receivecoupon(ev){
    var dataset=ev.currentTarget.dataset;
    var item=  dataset.item 
    var idx= dataset.idx
    var that = this
    var app =getApp();

    app.xc_sys_config.authop.oppower("login").then(function (res) {
     
      actcouponbill.receivecoupon(item.id).then(res=>{
        var updatemodel={}
        var keyval="modellist["+idx+"]."
        if(res["status"]==1){     
           updatemodel[keyval+"myreceivednum"]=res["obj"].myreceivednum
           updatemodel[keyval+"receivednum"]=res["obj"].receivednum
           
           
          that.setData(updatemodel)
         
          wx.showToast({
            title: '领取成功',
          })
        }
        else
        {
          if( res["status"]==-1){
            wx.showToast({
              title: res["message"],
            })
          }else if ( res["status"]==2){
            updatemodel[keyval+"receivednum"]=this.data.modellist[idx].num;
            that.setData(updatemodel)
          }
          else if ( res["status"]==3){
            updatemodel[keyval+"myreceivednum"]=this.data.modellist[idx].everyonelimit
            that.setData(updatemodel)
          }
  
        }
  
      })


    })
    
   
   


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
})